package cc.yihy.spider;

import cc.yihy.spider.core.*;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisShardInfo;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.scheduler.RedisScheduler;

import javax.annotation.PostConstruct;
import javax.management.JMException;

@Service
public class SpiderStart {

    @Autowired
    private SpiderConfigurationProperties properties;
    @Autowired
    private DBPipeline dbPipeline;
    @Autowired
    private JB51PageProcesser pageProcesser;
    @Autowired
    private JianshuDBPipeline jianshuDBPipeline;
    @Autowired
    private JianShuPageProcesser jianShuPageProcesser;
    @Autowired
    private SpiderHealthIndicator healthIndicator;

    @Autowired
    private RedisDataScheduler dataScheduler;

    @Getter
    private Spider spider;

    @PostConstruct
    public void init() {

        run();
    }

    @Async
    public void run() {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10 * 1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                 spider = Spider.create(jianShuPageProcesser)
                        .addUrl(properties.getStartUrl())
                         .setScheduler(dataScheduler)
                        .addPipeline(jianshuDBPipeline)
//                        .setDownloader(new NewHttpClientDownloader())
                        .thread(properties.getThreadCount());

                try {
                    SpiderMonitor.instance().register(spider);
                } catch (JMException e) {
                    e.printStackTrace();
                }

                healthIndicator.setSpider(spider);
                spider.run();
                spider.stop();
            }
        });

        thread.start();


    }
}
